package com.qingke.hibernate.oneday.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.qingke.hibernate.oneday.pojo.CourseImpl;

public class CourseImplDao {
	private Session session;

	public CourseImplDao(Session session) {
		this.session = session;
	}

	public List<CourseImpl> listCourseImpls() {
		Query<CourseImpl> query = session.createQuery("from Course_impl", CourseImpl.class);
		return query.getResultList();
	}

	public CourseImpl getCourseImplById(int id) {
		return session.get(CourseImpl.class, id);
	}

	public void insert(CourseImpl courseImpl) {
		Transaction ts = session.beginTransaction();
		try {
			session.save(courseImpl);
			session.flush();
			ts.commit();
		} catch (HibernateException he) {
			he.printStackTrace();
			ts.rollback();
		}
	}

	public void update(CourseImpl courseImpl) {
		Transaction ts = session.beginTransaction();
		try {
			session.update(courseImpl);
			ts.commit();
		} catch (HibernateException he) {
			he.printStackTrace();
			ts.rollback();
		}
	}

	public void delete(CourseImpl courseImpl) {
		Transaction ts = session.beginTransaction();
		try {
			session.delete(courseImpl);
			ts.commit();
		} catch (HibernateException he) {
			he.printStackTrace();
			ts.rollback();
		}
	}
}
